BMEN90033 · Week 9
Back
BMEN90033 · WEEK 9 · OVERVIEW

Comparators and timers in practice.

A comparator reduces a continuous voltage to a binary decision against a fixed reference. A timer converts an interval into a signal: either a single pulse of chosen duration, or a square wave at a chosen rate. Together these two blocks form the interface between the analog front end and the digital domain, and appear in almost every measurement instrument and stimulator. The scenes below illustrate six canonical uses; the underlying circuits and design equations are derived in the pages that follow.

comparators timers pulse generation threshold detection
scroll
// comparators
01battery low-voltage cutoff

Protecting a lithium cell from over-discharge.

A lithium-ion cell suffers permanent damage when it is discharged below approximately $3.0\,\mathrm{V}$. Wearable monitors, implanted pumps, and portable defibrillators therefore include a dedicated comparator whose role is to disconnect the load before this limit is reached.

The panel below traces the cell voltage as it discharges under load. A comparator continuously evaluates whether $V_\mathrm{cell}$ exceeds the cutoff and drives a single binary output to a power-path switch. While the cell remains above the threshold the load stays connected; once it falls below, the comparator flips and the switch opens.

Drag the dashed red line to change the cutoff voltage and observe the moment of disconnection.

Practical implementations add a small amount of hysteresis around the threshold so the comparator does not chatter on a noisy supply. That refinement is the Schmitt trigger, covered on the next page.
discharge rate 1.0 × running reset cell
cell voltage · Vcell(t) with comparator threshold
circuit · battery cutoff comparator
Vcell3.95 V
Vcutoff3.20 V
loadCONNECTED
02heart-rate detection

Counting R-peaks in a continuous ECG.

A bedside heart-rate monitor reports beats per minute, but its input is a continuous analog ECG. A comparator performs the conversion from waveform to beat. With its threshold placed above the T-wave but below the R-peak, the comparator emits one clean digital pulse on every upward crossing, and a downstream counter divides by the window length to obtain the rate.

Drag the red threshold line to adjust where the comparator fires. If it is set too low, T-waves and noise are counted as beats; if too high, real R-peaks are missed. The same arrangement appears in pulse oximeters, oscillometric blood-pressure cuffs, and event-driven sleep monitors.

A short refractory window follows each pulse to suppress further crossings, since the R-peak spans more than one sample and would otherwise produce several pulses per beat.
heart rate 72 bpm noise 0.06
ECG input · vin(t) and comparator output
true rate72 bpm
detected— bpm
threshold0.55
03ambient light switch

Switching a streetlight at dusk.

A photoresistor or photodiode produces a voltage proportional to incident light. A comparator placed at its output reduces the continuous reading to a binary day/night decision. The same circuit appears in automatic headlights, in the ambient sensor of a phone display, and in the standby logic of pulse-oximetry probes that sample only when a finger is present on the lens.

Drag the sun across the sky to vary the sensor voltage. When it falls below the dusk threshold, the comparator output goes high and the lamp switches on. The threshold marker on the right-hand bar sets how dark the scene must become before activation.

Without hysteresis, a passing cloud near the threshold would cause the lamp to flicker. A practical photo-switch uses a Schmitt-style comparator with separate on and off thresholds, so the lamp switches at most once per dusk and once per dawn.
scene · drag the sun; drag the threshold on the right
sensor0.80
threshold0.30
lampOFF
// timers
04cardiac pacing

Generating timed stimulation pulses.

An implanted pacemaker delivers a brief electrical pulse to the heart at a controlled rate. The timer decides when to fire, emitting one pulse of fixed width each time its internal interval expires. Pulse amplitude is set by a separate output stage; the timer concerns itself only with scheduling.

The same primitive appears in transcutaneous nerve stimulators (TENS), in cochlear implants that drive electrode arrays in synchrony, and in retinal prostheses. In every case the clinician selects a rate and a pulse width, and the timer renders those parameters as a periodic waveform.

Use the sliders to set the pacing rate and the pulse width. Each pulse corresponds to one paced heartbeat.

A timer that produces a continuous train of pulses is an astable multivibrator. A timer that emits a single pulse on demand and then waits is a monostable multivibrator. Both are constructed from a comparator with a charging capacitor; the design is derived later in the week.
pacing rate 72 ppm pulse width 0.8 ms
pacing output · vout(t) over a 4-second window
period T833 ms
duty0.10 %
heart
05pulse-width modulation

Setting average power with a duty cycle.

A timer that produces a square wave at fixed frequency but adjustable on-time can regulate average power without dissipating heat in a series resistance. The load sees the full supply during the on-interval of each cycle and zero during the off-interval, so its time-average is the supply voltage scaled by the duty cycle $D$.

The same primitive dims a phototherapy LED panel without colour shift, sets the motor speed of a syringe pump, and regulates the backlight of a clinical display. Class-D audio amplifiers apply it at a much higher carrier frequency to drive a speaker.

Move the duty-cycle slider and observe the load brightness track the time-average of the square wave. The lower plot traces the steady-state envelope of delivered power against PWM frequency: the green line is the time-average $D \cdot V_{cc}$, independent of frequency, and the shaded band is the peak-to-peak ripple at the bulb, modelled as a first-order thermal load with $\tau = 50\,\mathrm{ms}$. The live bulb at the right is driven by exactly this filtered waveform.

Below the bulb's thermal corner the ripple band is wide and visible flicker results. Above the corner the band collapses onto the average and the output reads as steady. The eye fuses an LED above roughly $100\,\mathrm{Hz}$, motor inertia smooths anything above a few hundred hertz, and a class-D amplifier operates at hundreds of kilohertz so that only the slow envelope reaches the speaker.
duty cycle 40 % frequency 5 Hz
PWM signal · vpwm(t) with running average
delivered power vs frequency · incandescent bulb, τ = 50 ms
live bulb
duty D40 %
avg load0.40 Vcc
ripple Δ— Vcc
flickervisible
06switch debouncing

Filtering a bouncing contact with $RC$ + Schmitt.

When a mechanical key is pressed, the metal contacts do not close cleanly. They strike, separate, and continue to bounce for several milliseconds before settling. To a digital input every closure appears as a fresh keypress, so one physical touch produces a burst of phantom characters.

The standard remedy is an $RC$ low-pass feeding a Schmitt-trigger comparator. The capacitor voltage obeys $\tau\,\dot V_C = V_\mathrm{in} - V_C$ with $\tau = RC$, so $V_C$ cannot follow the bouncing input directly; instead it integrates the burst. The Schmitt output goes high when $V_C$ crosses the upper threshold $V_{T+}$ and only resets after $V_C$ falls below the lower threshold $V_{T-}$. Choosing $\tau$ longer than the bounce interval guarantees that $V_C$ crosses $V_{T+}$ exactly once per physical press.

Click the input textbox and start typing. Each keypress is replaced by a realistic burst of contact bounces. The lower trace shows $V_C(t)$ integrating that burst, with the dashed band marking the Schmitt hysteresis window. Every $\mathrm{LOW}\!\to\!\mathrm{HIGH}$ crossing emits one character on the line; the tick above each crossing is green for the intended press and red for any phantom. At $T_d = 0$ the cap follows the bounces almost perfectly and many phantoms appear; raising $T_d$ (which sets $\tau = T_d$) smooths $V_C$ and removes them.

A typical key bounces for $1$ to $5\,\mathrm{ms}$. A time constant near $10\,\mathrm{ms}$ filters the bounces while remaining well below the inter-key interval of a fast typist (approximately $80\,\mathrm{ms}$). Too short a $T_d$ leaves residual phantoms; too long a $T_d$ suppresses genuine fast-repeated presses.
debounce Td 0 ms bounce severity 5 normal typing scroll mode clear
input · click and type one char per keypress
output
keypresses0
output chars0
phantom chars0
bounces rejected0